<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Shops extends Admin_Controller
{
    function __construct ()
    {
        parent::__construct();
        $this->load->model('shop_m');
        $this->load->model('areas_m');
        $this->load->model('good_m');
        $this->load->model('category_m');
    }
    //普通商家
    public function index()
    {
        $shop = $this->shop_m->get_all_shop_users();
        if(!empty($shop)){
            foreach($shop as $key=>$val){
                $area =  $this->areas_m->get_area_by_id($val['shop_city']);
                $shop[$key]['city_name'] = $area['area_name'];

            }
        }
        $category = explode('|', $this->config->item('web_category_txt'));
        $data['category'] = $category;
        $data['shop'] = $shop;
        $this->load->view('shop/index', $data);

    }
    //平台自营商家
    public function index2()
    {
        $shop = $this->shop_m->get_all_shop_self_users();
        if(!empty($shop)){
            foreach($shop as $key=>$val){
                $area =  $this->areas_m->get_area_by_id($val['shop_city']);
                $shop[$key]['city_name'] = $area['area_name'];

            }
        }
        $category = explode('|', $this->config->item('web_category_txt'));
        $data['category'] = $category;
        $data['shop'] = $shop;
        $this->load->view('shop/index', $data);

    }
    public function add( $shop = "",$step = 1)
    {   // 获取频道
        $category = explode('|', $this->config->item('web_category_txt'));
        $categoryId = explode('|', $this->config->item('web_category'));
        $data['category']=array_combine($categoryId,$category);
        // 获取所有的省
        $province = $this->areas_m->get_sons_by_pid();
        $data['province']=$province;
        $data['step'] = 1;
        if($step > 1 && !empty($shop)){
            $data['shop_id'] = $shop;
            $shopinfo = $this->shop_m->get_shop_user_by_id($shop);
            $data['shopcategory'] = $this->category_m->get_cates_by_all_category($shopinfo['web_category']);
        }
        $this->load->view('shop/form',$data);
    }
    public function edit($shop_id=0,$step = 1)
    {
        if ($shop_id) {
            $shop = $this->shop_m->get_shop_user_by_id($shop_id);
            // 获取频道
            $category = explode('|', $this->config->item('web_category_txt'));
            $categoryId = explode('|', $this->config->item('web_category'));
            $data['category']=array_combine($categoryId,$category);
            $data['shop'] = $shop;
            $data['step'] = $step;
            
            $data['shop_id'] = $shop_id;
            // 获取所有的省，及相应的市，区
            $province = $this->areas_m->get_sons_by_pid();

            $data['province']=$province;
            if(!empty($shop['shop_province'])){
                $city = $this->areas_m->get_sons_by_pid($shop['shop_province']);
                $data['city']=$city;
//                pp($city);exit();
            }
            if(!empty($shop['shop_city'])){
                $district = $this->areas_m->get_sons_by_pid($shop['shop_city']);
                $data['district']=$district;
            }
            //获取子分类
            $data['shopcategory'] = $this->category_m->get_cates_by_all_category($shop['web_category']);
//        pp($data);exit();
            $this->load->view('shop/form', $data);
        } else {
            show_message('参数错误',site_url(X_URL.'shop/index'),1);
        }

    }

    public function update()
    {
        $post = $this->input->post();
        $shopid = isset($post['shop_id']) ? intval($post['shop_id']) : false;

        if (!$shopid) {
            // 新增操作，先添加用户，再添加商家信息
            $username = isset($post['user_name']) ? htmlspecialchars($post['user_name'], ENT_QUOTES) : false;
            $getpassword= isset($post['user_password']) ? $post['user_password'] : false;
            if(!$username || !$getpassword){
                show_message('会员名和密码必须设置',site_url(X_URL.'shops/add'),1);
            }
            $salt =get_salt();
            $admin = array(
                'user_name' => $username,
                'user_password' =>  password_dohash($getpassword,$salt),
                'user_salt' => $salt,
                'user_credit' => $this->config->item('credit_start'),
                'user_regip' => get_onlineip(),
                'user_money'=> 0,
                'user_group'=> 3, // 暂时设置为3 商家，后期定义为常量
                'user_regtime' => time(),
                'user_logtime' => time(),
                'user_lock'=>0,
                'user_tel'=>$post['shop_tel'],
            );
            if(!$this->user_m->check_register_username($username)){
                $this->user_m->register($admin);
                $new_id = $this->db->insert_id();
            }
            if(!empty($new_id)){
                $this->user_m->update_user($new_id,array('user_key'=>password_dohash($new_id,$salt)));
                $this->groupAcess_m->insert_user_access(['uid'=>$new_id,'group_id'=>3]);
                // 新增商家信息
                $shop = array(
                    'user_id' => $new_id,
                    'web_category' =>  $post['web_category'],
                    'shop_title' => $post['shop_title'],
                    'shop_man' => $post['shop_man'],
                    'shop_tel' => $post['shop_tel'],
                    'shop_province'=>$post['province_id'],
                    'shop_city'=>$post['city_id'],
                    'shop_district'=>$post['district_id'],
                    'shop_dress'=>$post['shop_dress'],
                    'shop_lat'=>$post['shop_lat'],
                    'shop_lng'=>$post['shop_lng'],
                    'shop_time'=>time()
                );
                $shop_id = $this->shop_m->add_shop_user($shop);
                if($shop_id){
                    show_message('新增商家成功',site_url(X_URL.'shops/edit/'.$shop_id.'/2'),1);
                }else{
                    show_message('新增商家失败',site_url(X_URL.'shops/edit/'.$shop_id),1);
                }
            }else{
                show_message('新增用户失败',site_url(X_URL.'shops/index'),1);
            }
        } else {
            // 修改商家信息
            $shop = array(
                'web_category' =>  $post['web_category'],
                'shop_title' => $post['shop_title'],
                'shop_man' => $post['shop_man'],
                'shop_tel' => $post['shop_tel'],
                'shop_province'=>$post['province_id'],
                'shop_city'=>$post['city_id'],
                'shop_district'=>$post['district_id'],
                'shop_dress'=>$post['shop_dress'],
                'shop_lat'=>$post['shop_lat'],
                'shop_lng'=>$post['shop_lng'],
            );
            //查询单条数据的信息
            $shops = $this->shop_m->get_shop_user_by_id($shopid);
            $this->user_m->update_user($shops['user_id'],array('user_tel'=>$post['shop_tel']));
            $shop_id = $this->shop_m->sava_shop_user($shopid,$shop);
            $arr =[
                    ["shop_id"=>$shopid],
                    [
                    "shop_id"=>$shopid,
                    "good_province"=>$post['province_id'],
                    "good_city"=>$post['province_id'],
                    "good_district"=>$post['city_id'],
                    'good_lat'=>$post['shop_lat'],
                    'good_lng'=>$post['shop_lng']
                    ],
            ];
            $this->db->update_batch("shop_goods",$arr,"shop_id");

            if($shop_id){
                show_message('修改商家成功',site_url(X_URL.'shops/edit/'.$shopid.'/2'),1);
            }else{
                show_message('修改商家失败',site_url(X_URL.'shops/edit/'.$shopid),1);
            }

        }
    }

    public function update2(){
        $post = $this->input->post();
        $shop_id = isset($post['shop_id']) ? intval($post['shop_id']) : false;
        if ($shop_id) {
            $result = $this->shop_m->sava_shop_user($shop_id,['shop_memo'=>$post['shop_memo']]);
            if($result){
                show_message('配置商家详情成功',site_url(X_URL.'shops/edit/'.$shop_id.'/3'),1);
            }else{
                show_message('配置商家详情失败',site_url(X_URL.'shops/edit/'.$shop_id.'/2'),1);
            }
        }else{
            show_message('参数缺失',site_url(X_URL.'shops/edit'),1);
        }
    }

    public function update3(){
        $post = $this->input->post();
        $shop_id = isset($post['shop_id']) ? intval($post['shop_id']) : false;
        if ($shop_id) {
            // 平台自营
            if(isset($post['self_support']) && $post['self_support'] == "on"){
                $result = $this->shop_m->sava_shop_user($shop_id,['shop_about'=>$post['shop_about'],'shop_stock'=>$post['shop_stock'],'self_support'=>1,'shop_category'=>$post['shop_category']]);
            }else{
                $result = $this->shop_m->sava_shop_user($shop_id,['shop_about'=>$post['shop_about'],'shop_stock'=>$post['shop_stock'],'self_support'=>0,'shop_category'=>$post['shop_category']]);
            }
            
            if($result){
                show_message('商家设置成功',site_url(X_URL.'shops/index'),1);
            }else{
                show_message('商家设置失败',site_url(X_URL.'shops/edit/'.$shop_id.'/3'),1);
            }
        }else{
            show_message('参数缺失',site_url(X_URL.'shops/edit'),1);
        }
    }
    //商家搜索
    public function search(){
        $post = $this->input->post();
        $keyword = !empty($post['keyword']) ? $post['keyword'] : "";
        $order = !empty($post['order']) ? $post['order'] : "";
        $shop = $this->shop_m->get_all_shop_users_serch($keyword,$order);
        if(!empty($shop)){
            foreach($shop as $key=>$val){
                $area =  $this->areas_m->get_area_by_id($val['shop_city']);
                $shop[$key]['city_name'] = $area['area_name'];

            }
        }
        $category = explode('|', $this->config->item('web_category_txt'));
        $data['category'] = $category;
        $data['shop'] = $shop;
        $this->load->view('shop/index', $data);
    }
    //删除商家
    public function del($id){
        $data['title']="删除商家";
        $result = $this->shop_m->del_shop_user_by_id($id);
        if($result){
            show_message('删除商家成功！',site_url(X_URL.'shops/index'),1); 
        }
           
    }

}
